<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>电商管理后台</title>
    <link rel="shortcut icon" href="../favicon.ico">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../styles/common.css" />
    <link rel="stylesheet" href="../styles/index.css" />
    <link rel="stylesheet" href="../styles/icon/iconfont.css" />
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="地址编码" v-model="pagination.queryString" style="width: 200px;" class="filter-item" clearable></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="id" label="地址编码" align="center"></el-table-column>
                <el-table-column prop="userName" label="用户名称" align="center"></el-table-column>
                <el-table-column prop="name" label="地址名称" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增地址" :visible.sync="dialogFormVisible" @close="addDialogClosed">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="120px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名称" prop="userId">
                                    <el-select v-model="formData.userId" placeholder="请选择">
                                        <el-option v-for="(item,index) in User" :key="index" :label="item.username" :value="item.id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="地址名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑地址" :visible.sync="dialogFormVisible4Edit" @close="editDialogClosed">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="120px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名称" prop="userId">
                                    <el-select v-model="formData.userId" placeholder="请选择">
                                        <el-option v-for="(item,index) in User" :key="index" :label="item.username" :value="item.id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="地址名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../plugins/axios/axios.min.js"></script>
<script src="../js/request.js"></script>
<script src="../api/login.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:4,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            User:[],
            rules: {//校验规则
                name: [
                    { required: true, message: '地址名称为必填项', trigger: 'blur' },
                    { min: 1, max: 64, message: '地址名称在1~64个字符之间', trigger: 'blur'}
                ],
                userId: [{ required: true, message: '用户名称为必填项', trigger: 'blur' }]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
            this.getUser();
        },
        methods: {
            getUser(){
                axios.get("/user/findAll").then((res) => {
                    this.User = res.data.data;
                    console.log(this.User)
                })
            },
            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if(valid){
                        axios.put("/address/edit",this.formData).then((res) => {
                            if(res.data.flag){
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                            }else {
                                this.$message.error(res.data.message);
                            }
                        }).finally(() =>{
                            this.findPage();
                            this.dialogFormVisible4Edit=false;
                        });
                    }else {
                        this.$message.error("表单数据校验失败！");
                    }
                })
            },
            //添加
            handleAdd () {
                this.$refs['dataAddForm'].validate((valid) => {
                    if(valid){
                        console.log(this.formData);
                        axios.post("/address/add",this.formData).then((res) => {
                            this.dialogFormVisible=false;
                            if(res.data.flag){
                                this.findPage();
                                this.$message({
                                    message:res.data.message,
                                    type:'success'
                                })
                            }else {
                                this.$message.error(res.data.message);
                            }
                        })
                    }else {
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                })
            },
            //分页查询
            findPage() {
                var param={
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/address/pageQuery",param).then((res) => {
                    this.pagination.total=res.data.data.total;
                    this.dataList=res.data.data.records;
                }).catch((r)=> {
                    this.showMessage(r);
                });
            },
            // 重置表单
            resetForm() {
                this.formData={};
            },
            addDialogClosed(){
                this.$refs.dataAddForm.resetFields();
            },
            editDialogClosed(){
                this.$refs.dataEditForm.resetFields();
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible=true;
                this.resetForm();
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit=true;
                axios.get("/address/findById?id="+row.id).then((res) => {
                    if(res.data.flag){
                        this.formData=res.data.data;
                        this.dialogFormVisible4Edit=true;
                    }else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            showMessage(r){
                if (r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除当前数据吗？","提示",{
                    type: 'warning'
                }).then(() => {
                    axios.delete("/address/delete?id="+row.id).then((res) => {
                        if(res.data.flag){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                            this.findPage();
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type:'info',
                        message:"操作已取消"
                    });
                })
            },
        }
    })
</script>
</html>